package com.wpzf.zxhc.service.impl;

import com.wpzf.zxhc.domain.WpCjDkxxbxj2023DTO;
import com.wpzf.zxhc.domain.WpCjTbhcjgshij2023;
import com.wpzf.zxhc.domain.WpCjTbxxbxj2023DTO;
import com.wpzf.zxhc.domain.YtbTbInfo;
import com.wpzf.zxhc.mapper.DkShCityMapper;
import com.wpzf.zxhc.mapper.XjtbTbBaseMapper;
import com.wpzf.zxhc.service.DkShCityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DkShCityServiceImpl implements DkShCityService {
    @Autowired
    DkShCityMapper dkShCityMapper;

    @Autowired
    XjtbTbBaseMapper xjtbTbBaseMapper;

    @Override
    public List<Map> getCityDkShPch(Map<String, Object> param) {
        return dkShCityMapper.getCityDkShPch(param);
    }

    @Override
    public List<Map> getCityDkShInfo(Map<String, Object> param) {
        return dkShCityMapper.getCityDkShInfo(param);
    }

    @Override
    public List<Map> getDkShInfoByXzq(Map<String, Object> param) {
        return dkShCityMapper.getDkShInfoByXzq(param);
    }

    @Override
    public List<Map> getDkShInfoByXzqIds(Map<String, Object> param) {
        return dkShCityMapper.getDkShInfoByXzqIds(param);
    }

    @Override
    public List<Map> getLeftData(Map<String, Object> param) {
        return dkShCityMapper.getLeftData(param);
    }

    @Override
    public WpCjDkxxbxj2023DTO getDkInfoByDkbh(Map<String, Object> map) {
        WpCjDkxxbxj2023DTO dkInfoByDkbh = dkShCityMapper.getDkInfoByDkbh(map);
        if ("符合法律法规和相关政策用地".equals(dkInfoByDkbh.getPdlx())) {
            //dkInfoByDkbh.setHfpwList(dkShCityMapper.queryWpCjDkxxbxjHfpw2023ByDkbh(dkInfoByDkbh));
        }
        return dkInfoByDkbh;
    }

    @Override
    public List<WpCjDkxxbxj2023DTO> getRelationDkInfoByJcbh(Map<String, Object> map) {
        return dkShCityMapper.getRelationDkInfoByJcbh(map);
    }

    @Override
    public List<WpCjTbxxbxj2023DTO> getTbInfoByJcbh(Map<String, Object> map) {
        return dkShCityMapper.getTbInfoByJcbh(map);
    }

    @Override
    public Map getCityDkHcJgInfo(Map<String, Object> param) {
        return dkShCityMapper.getCityDkHcJgInfo(param);
    }

    @Override
    public Integer deleteDkHcJg(Map<String, Object> param) {
        return dkShCityMapper.deleteDkHcJg(param);
    }

    @Override
    public Integer selectDkHcJg(Map<String, Object> param) {
        return dkShCityMapper.selectDkHcJg(param);
    }

    @Override
    public Integer updateDkHcJg(Map<String, Object> param) {
        return dkShCityMapper.updateDkHcJg(param);
    }

    @Override
    public Integer insertDkHcJg(Map<String, Object> param) {
        return dkShCityMapper.insertDkHcJg(param);
    }

    @Override
    public Integer insertOperationFlow(Map<String, Object> param) {
        return dkShCityMapper.insertOperationFlow(param);
    }

    @Override
    public Integer updateDkzt(Map<String, Object> param) {
        return dkShCityMapper.updateDkzt(param);
    }

    @Override
    public Integer returnDkHcXj(Map<String, Object> param) {
        String sDkbh = param.get("dkbh") + "";
        String[] arrDkbh = sDkbh.split(",");
        Integer nSuccess = 0;
        for (String dkbh : arrDkbh) {
            param.replace("dkbh", dkbh);
            nSuccess += dkShCityMapper.updateDkzt(param);
        }
        return nSuccess;
    }

    @Override
    public Integer citySubmit(Map<String, Object> param) {
        /**
         * 是否有未通过数据
         * 更新流转记录
         * 更新地块状态*/
        param.put("dkzt", "3");
        Integer nBtg = dkShCityMapper.getBtgDkCount(param);
        Integer nInsert = -1;
        Integer nUpDkzt = -1;
        Integer nUpSftj = -1;
        if (nBtg == 0) {
            nInsert = dkShCityMapper.insertOperationFlow(param);
            nUpSftj = dkShCityMapper.updateSftjByXzq(param);
            nUpDkzt = dkShCityMapper.updateDkztByXzq(param);
        }
        return nUpDkzt;
    }

    @Override
    public List<Map> getProvBackPch(Map<String, Object> param) {
        return dkShCityMapper.getProvBackPch(param);
    }

    @Override
    public List<Map> getProvBackDkInfo(Map<String, Object> param) {
        return dkShCityMapper.getProvBackDkInfo(param);
    }

    @Override
    public List<Map> getDkFlowInfo(Map<String, Object> param) {
        return dkShCityMapper.getDkFlowInfo(param);
    }

    @Override
    public List<Map> getXjZgDkInfo(Map<String, Object> param) {
        return dkShCityMapper.getXjZgDkInfo(param);
    }

    @Override
    public List<Map> getXjZgDkInfoIds(Map<String, Object> param) {
        return dkShCityMapper.getXjZgDkInfoIds(param);
    }

    @Override
    public List<Map> getXjZgDkPch(Map<String, Object> param) {
        return dkShCityMapper.getXjZgDkPch(param);
    }

    @Override
    public List<Map> getXjZgDkLeftData(Map<String, Object> param) {
        return dkShCityMapper.getXjZgDkLeftData(param);
    }

    @Override
    public Map getDkHcJgInfoXjZg(Map<String, Object> param) {
        Map map = dkShCityMapper.getCityDkHcJgInfo(param);
        if (map == null)
            return map;
        if (map.containsKey("ecsfhc")) {
            if ((map.get("ecsfhc") + "").isEmpty() || map.get("ecsfhc").equals("")) {
                map.replace("hcjl", "");
                map.replace("hcsm", "");
            }
        } else {
            map.replace("hcjl", "");
            map.replace("hcsm", "");
        }
        return map;
    }

    @Override
    public Integer updateDkHcJgXjZg(Map<String, Object> param) {
        return dkShCityMapper.updateDkHcJgXjZg(param);
    }

    @Override
    public Integer updateDkHcJgTjByXjZg(Map<String, Object> param) {
        return dkShCityMapper.updateDkHcJgTjByXjZg(param);
    }

    @Override
    public Integer xjZgSubmit(Map<String, Object> param) {
        /**
         * 是否有未通过数据
         * 更新流转记录
         * 更新地块状态*/
        Integer nInsert = dkShCityMapper.insertOperationFlow(param);
        Integer nUpDkzt = dkShCityMapper.updateDkztByXjZg(param);
        Integer nUpSftj = dkShCityMapper.updateDkHcJgTjByXjZg(param);
        return nUpDkzt;
    }

    @Override
    @Transactional
    public Integer saveShiTbjg(Map<String, Object> param) {
        dkShCityMapper.deleteDkHcJg(param);
        param.put("hcsj", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
        return dkShCityMapper.saveShiTbjg(param);
    }

    @Override
    public WpCjTbxxbxj2023DTO getTbxxbxjByJcbh(Map<String, Object> param) {
        return dkShCityMapper.getTbxxbxjByJcbh(param);
    }

    @Override
    public List<WpCjTbxxbxj2023DTO> getTbxxbxjByJcbhList(Map<String, Object> param) {
        return dkShCityMapper.getTbxxbxjByJcbhList(param);
    }

    @Override
    public WpCjTbhcjgshij2023 getTbhcjgshijByDkbh(Map<String, Object> param) {
        return dkShCityMapper.getTbhcjgshijByDkbh(param);
    }

    @Override
    @Transactional
    public Integer shiSubmit(Map<String, Object> param) {
        Integer rest = 0;
        dkShCityMapper.deleteDkHcJg(param);
        param.put("hcsj", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
        param.put("sftj", "是");
        param.put("tjsubmitdate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
        rest = dkShCityMapper.shiSubmit(param);
//        param.put("dkzt", "3");
        xjtbTbBaseMapper.updateDkxxbDkzt(param);
        param.put("czsj", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
        param.put("result", param.get("hcjl"));
        param.put("sm", param.get("hcsm"));
        param.put("userlevel", "市");
        xjtbTbBaseMapper.saveDkxxbxjlz(param);
        return rest;
    }

    @Override
    public Integer shiThxg(Map<String, Object> param) {
        Integer rest = 0;

        param.put("czsj", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
        param.put("result", param.get("hcjl"));
        param.put("sm", param.get("hcsm"));
        param.put("userlevel", "市");
        xjtbTbBaseMapper.saveDkxxbxjlz(param);
        param.put("dkzt", "4");
        param.put("thsj", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
        dkShCityMapper.updateDkzt(param);

        return rest;
    }

    @Override
    public List<YtbTbInfo> queryShiTask(Map<String, Object> parm) {
        return dkShCityMapper.queryShiTask(parm);
    }

    @Override
    public List<YtbTbInfo> queryShiTaskEX(Map<String, Object> parm) {
        List<YtbTbInfo> ytbTbInfoList = dkShCityMapper.queryShiTaskEX(parm);
        for (YtbTbInfo ytbTbInfo : ytbTbInfoList) {
            Map<String, Object> map = new HashMap<>();
            map.put("dkbh", ytbTbInfo.getDkbh());
            map.put("xzqdm", ytbTbInfo.getXzqdm());
            map.put("pch", ytbTbInfo.getPch());
            Map<String, Object> shiData = xjtbTbBaseMapper.queryShi(map);
            if (shiData != null) {
                ytbTbInfo.setShiHcjl(shiData.get("hcjl") + "");
                ytbTbInfo.setShiHcsm(shiData.get("hcsm") + "");
                ytbTbInfo.setShiUsername(shiData.get("username") + "");
                ytbTbInfo.setShiRealname(shiData.get("realname") + "");
            }

            Map<String, Object> shengData = xjtbTbBaseMapper.querySheng(map);
            if (shengData != null) {
                ytbTbInfo.setShengHcjl(shengData.get("hcjl") + "");
                ytbTbInfo.setShengHcsm(shengData.get("hcsm") + "");
                ytbTbInfo.setShengUsername(shengData.get("username") + "");
                ytbTbInfo.setShengRealname(shengData.get("realname") + "");
            }
        }
        return ytbTbInfoList;
    }

    @Override
    public List<YtbTbInfo> queryShiTaskZG(Map<String, Object> parm) {
        return dkShCityMapper.queryShiTaskZG(parm);
    }

    @Override
    public List<YtbTbInfo> queryShiTaskZGEX(Map<String, Object> parm) {
        return dkShCityMapper.queryShiTaskZGEX(parm);
    }

    @Override
    @Transactional
    public Integer shiSubmitZG(Map<String, Object> param) {
        Integer rest = 0;
        dkShCityMapper.deleteDkHcJg(param);
        param.put("hcsj", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
        param.put("sftj", "是");
        param.put("tjsubmitdate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
        rest = dkShCityMapper.shiSubmit(param);
        param.put("dkzt", "6");
        xjtbTbBaseMapper.updateDkxxbDkzt(param);
        param.put("czsj", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
        param.put("result", param.get("hcjl"));
        param.put("sm", param.get("hcsm"));
        param.put("userlevel", "市");
        xjtbTbBaseMapper.saveDkxxbxjlz(param);
        return rest;
    }

    @Override
    public List<Map<String, Object>> queryShiBtgdk(Map<String, Object> parm) {
        return dkShCityMapper.queryShiBtgdk(parm);
    }

    @Override
    public Integer queryShiBtgdkCount(Map<String, Object> parm) {
        return dkShCityMapper.queryShiBtgdkCount(parm);
    }
}
